<template>
  <div class="login-page">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar">
        <img src="../assets/img/panada.jpeg" alt="" />
      </div>

      <!-- 表单区域 -->
      <!-- :model整个表单的数据 -->
      <el-form
        ref="loginFromRef"
        class="login-from"
        :rules="loginFromRules"
        :model="loginForm"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <!-- 输入密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-suo"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" round @click="login">登陆</el-button>
          <el-button type="info" round @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
console.log('hello world')
export default {
  data() {
    return {
      // 登陆表单的数据对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 校验规则
      loginFromRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 7, message: '用户名需在 2 至 7 位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created() {},

  methods: {
    // 点击重置按钮
    resetLoginForm() {
      this.$refs.loginFromRef.resetFields()
    },
    login() {
      this.$refs.loginFromRef.validate(async flag => {
        if (!flag) return
        // console.log('发送请求')
        const res = await this.$axios.post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('用户名或密码不正确')
        }
        // console.log(res)
        this.$message.success('登录成功')
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.login-page {
  width: 100%;
  height: 100%;
  // background: url(../assets/img/backgroundLogin.jpg) no-repeat center;
  // background-size: cover;
  background-color: lightblue;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    width: 400px;
    height: 290px;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 10px;
    position: relative;
    .avatar {
      position: absolute;
      top: -50px;
      left: 50%;
      transform: translateX(-50%);
      width: 100px;
      height: 100px;
      border-radius: 50%;
      background-color: #999;
      display: flex;
      justify-content: center;
      box-shadow: 0px 0px 10px #999;
      align-items: center;
      img {
        width: 99%;
        height: 99%;
        border-radius: 50%;
      }
    }
    .login-from {
      margin-top: 80px;
      padding: 0 15px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
